os.loadAPI("/System/API/sysbar")
local pos = 0
local xSize, ySize = term.getSize()
local files = fs.list("/")
local screen = window.create(term.current(),1,2,xSize-1,ySize-2)
local pos = 1
local run = false
local history = {}
local hisPos = 0
history[0] = ""
local inp = ""
shell.setDir("")

local clear = function(color)
  term.setBackgroundColor(color)
  term.clear()
  term.setCursorPos(1,1)
end

local function drawBar()
  term.redirect(term.native())
  sysbar.draw(colors.black,colors.lightGray,noty,true,false,false)
  term.redirect(screen)
end

local function redraw()
  clear(colors.black)
end

local function  running()
  while true do
    local event, side, x, y = os.pullEventRaw()
    if event == "mouse_click" then
      term.redirect(term.native())
			if sysbar.back(x,y) == true then
        if run then
          term.setBackgroundColor(colors.black)
          term.clear()
          term.redirect(screen)
          term.clear()
				  os.queueEvent("terminate")
          run = false
          term.redirect(term.native())
          drawBar()
        else
          term.clear()
				  term.setCursorPos(1,1)
				  break
        end
			elseif sysbar.home(x,y) == true then
				term.clear()
				term.setCursorPos(1,1)
				home = true
				break
      end
    elseif event == "mouse_scroll" then
      if not run then
        term.redirect(screen)
        local xp,yp = term.getCursorPos()
        if side == 1 then
          term.scroll(1)
          term.setCursorPos(4,yp-1)
        elseif side == -1 then
          term.scroll(-1)
          term.setCursorPos(4,yp+1)
        end
        term.redirect(term.native())
      end
    elseif event == "key" then
      term.redirect(screen)
      local xp,yp = term.getCursorPos()
      if side == keys.down then
        if hisPos > 0 then
          hisPos = hisPos - 1
        end
        inp = history[hisPos]
        paintutils.drawLine(4,yp,xSize,yp,colors.black)
        term.setCursorPos(4,yp)
        term.write(inp)
        term.setCursorPos(4+#inp,yp)
      elseif side == keys.up then
        if hisPos < #history then
          hisPos = hisPos + 1
        end
        inp = history[hisPos]
        paintutils.drawLine(4,yp,xSize,yp,colors.black)
        term.setCursorPos(4,yp)
        term.write(inp)
        term.setCursorPos(4+#inp,yp)
      end
    end
  end
end

local function input()
  while true do
    term.redirect(screen)
    local xp,yp = term.getCursorPos()
    term.redirect(term.native())
    paintutils.drawLine(1,1,xSize,1,colors.black)
    if shell.dir() == "System" or shell.dir() == ".ds" then
      shell.setDir("")
      term.redirect(screen)
      term.setTextColor(colors.red)
      term.write("Access Denied!")
      yp = yp+1
      term.redirect(term.native())
    end
    term.setTextColor(colors.lightGray)
    term.setCursorPos(1,1)
    term.write("/"..shell.dir())
    term.redirect(screen)
    files = fs.list("/"..shell.dir())
    term.setBackgroundColor(colors.black)
    term.setTextColor(colors.red)
    term.setCursorPos(2,yp)
    term.write("> ")
    term.setTextColor(colors.white)
    local comm = read()
    history[#history+1] = inp..comm
    run = true
    shell.run(inp..comm)
    run = false
    inp = ""
    term.redirect(term.native())
    drawBar()
  end
end

redraw()
drawBar()
term.setCursorPos(2,1)
parallel.waitForAny(running, input)